import router from '@system.router';
import fetch from '@system.fetch';
import prompt from '@system.prompt';
//import http from '@ohos.net.http';
var counter = 10;
var timer = null;
var tar=1;
var cat=3;
export default {
    data: {
        types:'Muscle',
        status:'ON',
        kneading_id:'8',
        seconds:'0',
        write:'100',
        progress: 10,
        vals1:"待测",
        vals2:"待测",
        nowDate:"",
        winfo:"",

        //        time:"2022-04-22 10:56:08",
        startText:"开始检测",
//        lineData: [
//            {
//                strokeColor: '#0081ff',
//                fillColor: '#cce5ff',
//                data: [],
//                gradient: true,
//            }
//        ],
//        lineOps: {
//            xAxis: {
//                //                min: -3600,
//                //                max: -3400,
//                min:0,
//                max: 20,
//                axisTick:0,
//                color:`#353535`,
//                display: true,
//            },
//            yAxis: {
//                min: -1,
//                max:1,
//                axisTick:false,
//                color:`#353535`,
//                display: true,
//            },
//            series: {
//                lineStyle: {
//                    width: "3px",
//                    smooth: false,
//                },
//                headPoint: {
//                    shape: "circle",
//                    size: 10,
//                    strokeWidth: 5,
//                    fillColor: '#ffffff',
//                    strokeColor: '#007aff',
//                    display: true,
//                },
//                loop: {
//                    margin: 2,
//                    gradient: true,
//                }
//            }
//        },
    },
    run(){
        counter--;
        if (tar==2&&counter!=-1) {
            this.seconds = counter.toString();
            cat=counter.toString();
            this.write=cat*10;
            if(cat==0){
                this.startText="重新测试"
                tar=3;
            }
        } else{
            this.seconds = "0";
            clearInterval(timer);
            timer = null;
            console.info(this.Account)
            console.info(JSON.stringify({"types":this.types,"status":this.status,"kneading_id":this.kneading_id}));
            fetch.fetch({
                url:`http://49.4.115.226:8000/get_specified_muscle_data?date=`+this.nowDate,
                //http://49.4.115.226/get_bend_data
                method: 'POST',
                headers: {
                    'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/',
                    'Content-Type': 'application/json'
                },
                responseType:"json", //请求的参数类型
                //如果获取数据成功的话执行以下函数
                success:(resp)=>
                {
                    this.winfo = JSON.parse(resp.data);  //令获取到的数据赋给winfo
                    var resp_muscle = JSON.stringify(this.winfo.results[0].Muscle.muscle);
                    prompt.showToast({
                        message:resp_muscle,
                        duration:3000
                    });
                    this.vals2 = resp_muscle;
                    this.vals1 = 8;
                },
                //如果获取数据失败则执行以下函数
                fail:(resp)=>
                {
                    this.winfo = resp.data;
                    console.info("获取数据失败："+this.winfo)
                }
            });
            tar=1;
            counter = 10;
        }
    },
    launch3() {
        //发起网络请
        if(tar==1){
            tar=2;
            var date1 = new Date();
            var nowMonth = date1.getMonth() + 1;
            var strDate = date1.getDate();
            // 添加分隔符“-”
            var seperator = "-";

            // 对月份进行处理，1-9月在前面添加一个“0”
            if (nowMonth >= 1 && nowMonth <= 9) {
                nowMonth = "0" + nowMonth;
            }

            // 对月份进行处理，1-9号在前面添加一个“0”
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }

            // 最后拼接字符串，得到一个格式为(yyyy-MM-dd)的日期
            this.nowDate = nowMonth + seperator + strDate;
            console.info(this.nowDate);
            // 获取当前是几号
            clearInterval(timer);
            this.seconds = counter.toString();
            timer = setInterval(this.run, 1000);
            this.startText="检测中..."
            console.info(this.Account)
            console.info(JSON.stringify({"types":this.types,"status":this.status,"kneading_id":this.kneading_id}));
            fetch.fetch({
                url:`http://49.4.115.226:8000/get_command?types=`+this.types+'&status='+this.status,//资源地址
                //设置header伪装
                method: 'POST',
                headers: {
                    'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/',
                    'Content-Type': 'application/json'
                },
                responseType:"json", //请求的参数类型
                //如果获取数据成功的话执行以下函数
                success:(resp)=>
                {
                    this.winfo = resp.data;  //令获取到的数据赋给winfo
                    console.info("返回的数据："+this.winfo)//打印出数据
                },
                //如果获取数据失败则执行以下函数
                fail:(resp)=>
                {
                    this.winfo = resp.data;
                    console.info("获取数据失败："+this.winfo)
                }
            });
        }else if(tar==3){
            this.startText="开始检测"
            tar=1;
        }
        else{
            this.startText="重新检测"
        }
    },
    launch1() {
        router.push ({
            uri: 'pages/action3/action3',
        });
    },
    launch2() {
        router.push ({
            uri: 'pages/result/result',
        });
    },
    launch4() {
        router.push ({
            uri: 'pages/niam/niam',
        });
    },
}
